<template>
	<div class="buycart-main">
		<checkbox-group @change="ckChange">
			<view class="product-card" v-for="item in productList" :key="item.skuId">

				<view class="info-block">
					<label>
						<checkbox :value="item.skuId" :checked="item.checked" /><text></text>
					</label>
					<navigator class="nav" url="/pages/buyCart/index">
						<image class="cover-img" :src="item.coverImg" mode=""></image>
						<view class="info-text">
							<view class="gname">{{item.gname}}</view>
							<view class="code">
								<text>编号: {{item.skuCode}}</text>
								<text>价格: ¥{{item.price | money}}</text>
							</view>
							<view>数量: {{item.count}}</view>
						</view>
					</navigator>
				</view>

				<view class="buy-count-steps">
					<text class="step-reduce" @tap="btnReduce(item)">-</text>
					<text class="step-text">{{item.count}}</text>
					<text class="step-add" @tap="btnAdd(item)">+</text>
				</view>
			</view>
		</checkbox-group>

		<view class="footer-bar">
			<view class="sel-all">
				<checkbox-group @change="selAll">
					<label>
						<text class="text">全选</text>
						<checkbox value="1" :checked="allChecked" />
					</label>
				</checkbox-group>
			</view>
			<view class="price">
				合计: ¥ <text class="text">{{allPrice | money}}</text>
				<text class="btn" @click="goPay">结算</text>
			</view>
		</view>
	</div>
</template>

<script>
	export default {
		name: 'BuyCart',
		components: {

		},
		data() {
			return {
				isSelAll: false,
				productList: [],
				checkbox: [],
			};
		},
		onLoad() {
			this.productList = uni.getStorageSync('leju-buycart');
			for (var i = 0; i < this.productList.length; i++) {
				// 这样初始化 这是非响应式的
				// this.productList[i].checked = false;
				this.$set(this.productList[i], 'checked', false)
			}
			 
		},
		computed: {
			allChecked() {
				if(this.productList.length==0){
					return false;
				}
				// every 判断每一项的checked = true
				return this.productList.every(item => item.checked);
			},
			allPrice() {
				if(this.productList.length == 0) return 0;
				return this.productList.reduce((a, b) => {
					if (b.checked) {
						a += b.count * b.price;
					}
					return a;
				}, 0)
			}

		},
		mounted() {

		},
		methods: {
			btnReduce(item) {
				if (item.count > 1){
					item.count--;
				}else{
					uni.showModal({
						title: '删除该记录?',
						confirmText:'确认',
						cancelText: '取消',
						success(res){
							if(res.confirm){
								alert('ok')
							} else{
								alert('取消了')
							}
						}
					})
				}
			},
			btnAdd(item) {
				item.count++;

			},
			ckChange(e) {
				// 获取所有的checkbox的结果 是一个数组
				var values = e.detail.value;
				
				// 由于checkbox没有双向响应 需要手动修改状态
				for (var i = 0; i < this.productList.length; i++) {
					var item = this.productList[i];
					// includes 判断包含  返回boolean
					item.checked = values.includes(item.skuId);
				}
			},
			goPay() {
				// 1.构建对象
				var skuList = [];
				var skuList = this.productList.filter(item=>item.checked);
				// 总数量
				var totalCount = skuList.reduce((a,b)=>{
					a += b.count;
					return a;
				},0);
				// 总价格
				var totalPrice = this.allPrice;
				
				 
				//构建订单对象
				var orderObj = {
					userId: this.$store.getters.userId,
					count: totalCount, // 因为商品详情页 只有一个sku 所以sku的count就是总count
					freight: 0, // 运费 , 见api
					price: totalPrice, // 总价格
					orderDetailList: skuList
				}
				//存入vuex
				this.$store.commit('order/ADD_ORDER_TEMP', orderObj)

				// 跳转到订单确认页面
				uni.navigateTo({
					url: '/pages/mine/order/detail/index?from=cart'
				})

			},
			selAll(e) {
				var value = e.detail.value;
				// console.log(value)
				for (var i = 0; i < this.productList.length; i++) {
					// 如果选中 都为true  否则都为false
					this.productList[i].checked = value.length > 0;
				}

			}
		},
	};
</script>

<style scoped lang="scss">
	.buycart-main {
		.product-card {
			width: 672rpx;
			margin: 50rpx auto;
			background-color: #fff;
			border-radius: 24rpx;
			padding: 20rpx 20rpx;
			box-sizing: border-box;

			.info-block {
				display: flex;
				justify-content: flex-start;
				align-items: center;

				.nav {
					display: flex;
					justify-content: flex-start;
					align-items: center;

					.cover-img {
						width: 136rpx;
						height: 136rpx;
						flex-shrink: 1;
					}

					.info-text {
						font-size: 22rpx;
						color: #8D8D8D;
						margin-left: 46rpx;

						.gname {
							font-size: 26rpx;
							color: #3E3E3E;
						}

						.price {
							display: flex;
							justify-content: space-between;
							align-items: center;
						}

					}
				}

			}

			.buy-count-steps {
				display: flex;
				justify-content: flex-end;

				.step-reduce,
				.step-add,
				.step-text {
					width: 60rpx;
					height: 60rpx;
					background: #F1ECE7;
					border-radius: 50%;
					text-align: center;
					line-height: 60rpx;
					font-size: 30rpx;
					margin: 0 6rpx;
				}

				.step-text {
					width: 90rpx;
					border-radius: 30rpx;
				}
			}
		}

		.footer-bar {
			position: fixed;
			display: flex;
			justify-content: space-around;
			align-items: center;
			left: 0;
			bottom: 0;
			width: 100%;
			height: 150rpx;
			background-color: #fff;

			.btn {
				display: inline-block;
				width: 208rpx;
				height: 80rpx;
				background: #354E44;
				border-radius: 14px;
				color: #fff;
				font-size: 36rpx;
				line-height: 80rpx;
				text-align: center;
			}

			.sel-all {
				display: flex;
				justify-content: flex-start;
				align-items: center;

				.text {
					margin-right: 20rpx;
				}
			}

			.price {
				.text {
					color: red;
					margin-right: 10rpx;
				}
			}
		}
	}
</style>
